import { IModule } from "../common/Common";
import game from "../game";
import { Fview, UILayer } from "../gui/UIDefine";
import { Logger } from "../log/logger";
import Guide from "./Guide";
import { GuideInfo, GuideType } from "./GuideDefine";

export interface GuideOption {
    /**关闭旧引导 */
    closeOld?: boolean;

    /**完成回调 */
    callback?: Function;
}

export default class GuideManage implements IModule {

    private _guideView: Guide;
    private _currentGuideList: GuideInfo[] = [];

    private _currGuide: GuideInfo;
    private _callBack: Function;


    initialize(): void {
        Logger.logBusiness("初始化引导")
    }

    /**
     * 新建引导
     * @param info 
     * @param closeOld 是否关闭旧引导 
     * @param callback 结束回调
     */
    newGuide(info: GuideInfo[], option?: Partial<GuideOption>) {

        option = option || {};

        //有引导
        if (this._currentGuideList.length > 0 || this._currGuide) {
            if (option.closeOld) {
                this.closeCurrentGuide();
            } else {
                Logger.logBusiness("引导已经存在");
                return;
            }
        }


        this._currentGuideList = info;
        this._callBack = option.callback;
        game.openui(Guide, {
            layer: UILayer.guide,
            openHandler: Laya.Handler.create(this, this.next),
            permanent: true,
            bgMask: false

        })

    }


    next() {

        let info = this._currentGuideList.shift();

        if (!info) {
            this.closeCurrentGuide();
            Logger.logBusiness({ code: -1, msg: "引导结束" }, "引导");
            return;
        }

        let targetView = game.ui.getOpenUI(info.view);
        if (!targetView) {
            this.closeCurrentGuide();
            Logger.logBusiness({ code: -2, msg: "引导目标ui未打开" }, "引导");
            return;
        }

        let target: Fview = info.target instanceof Function ? info.target() : targetView[info.target];
        if (!target) {
            this.closeCurrentGuide();
            Logger.logBusiness({ code: -3, msg: "引导ui未找到目标" }, "引导");
            return;
        }

        if (!this._guideView) {
            this._guideView = game.ui.getOpenUI(Guide) as Guide;
        }

        if (!this._guideView || !this._guideView.opened) {
            this.closeCurrentGuide();
            Logger.logBusiness({ code: -4, msg: "引导ui未打开" }, "引导");
            return;
        }

        let tyep = info.type || GuideType.CLICK;
        if (tyep === GuideType.CLICK) {
            target.displayObject.once(Laya.Event.CLICK, this, this.next.bind(this));
        }

        this._guideView.next(info);
    }


    //关闭当前引导
    closeCurrentGuide(eventCallback: boolean = true) {
        this._currentGuideList.length = 0;

        if (this._guideView && this._guideView.opened) {
            this._guideView.close();
            this._guideView = null;
        }

        if (this._callBack) {
            eventCallback && this._callBack();
            this._callBack = null;
        }
    }
}